package tests;

import common.Utils;
import org.junit.jupiter.api.Assertions;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.support.ui.ExpectedConditions;

/**
 * 管理员注册页面
 */
public class AdminRegisterPage extends Utils {

    private static final String url = "http://82.156.185.199:8080/register.html?admin=true";

    public AdminRegisterPage() {
        super(url);
    }

    // 检查页面是否可以正常打开
    public void register(){
        // "填写注册信息"
        driver.findElement(By.cssSelector("body > div > h2"));
        // "手机号"
        driver.findElement(By.cssSelector("#registerForm > div:nth-child(3) > label"));
        // "手机号" 输入框
        driver.findElement(By.cssSelector("#phoneNumber"));
        // "注册" 按钮
        driver.findElement(By.cssSelector("#registerForm > button"));
    }

    // 注册失败
    public void registerFail(){
        // 都不填写
        driver.findElement(By.cssSelector("#registerForm > button")).click();
        driver.findElement(By.cssSelector("#name-error"));
        driver.findElement(By.cssSelector("#name-error"));
        driver.findElement(By.cssSelector("#phoneNumber-error"));
        driver.findElement(By.cssSelector("#password-error"));

        // 填写已注册过的手机号
        // 刷新页面
        driver.navigate().refresh();
        driver.findElement(By.cssSelector("#name")).sendKeys("赵六");
        driver.findElement(By.cssSelector("#mail")).sendKeys("166@qq.com");
        driver.findElement(By.cssSelector("#phoneNumber")).sendKeys("15714393906");
        driver.findElement(By.cssSelector("#password")).sendKeys("123456");
        driver.findElement(By.cssSelector("#registerForm > button")).click();
        alterHandle();

        // 手机号格式错误
        // 清空手机号
        driver.findElement(By.cssSelector("#phoneNumber")).clear();
        driver.findElement(By.cssSelector("#phoneNumber")).sendKeys("1661111111");
        driver.findElement(By.cssSelector("#registerForm > button")).click();
        alterHandle();

        // 密码格式错误
        // 先清空手机号和邮箱，输入正确的手机号 + 错误的邮箱号
        driver.findElement(By.cssSelector("#phoneNumber")).clear();
        driver.findElement(By.cssSelector("#password")).clear();
        driver.findElement(By.cssSelector("#phoneNumber")).sendKeys("16611111111");
        driver.findElement(By.cssSelector("#password")).sendKeys("12345");
        driver.findElement(By.cssSelector("#registerForm > button")).click();
        // 提示 "密码长度至少为6个字符"
        driver.findElement(By.cssSelector("#password-error"));
    }

    // 注册成功
    public void registerSuc() throws InterruptedException {
        driver.navigate().refresh();
        driver.findElement(By.cssSelector("#name")).sendKeys("旺旺碎冰冰5");
        driver.findElement(By.cssSelector("#mail")).sendKeys("195@qq.com");
        driver.findElement(By.cssSelector("#phoneNumber")).sendKeys("19511111111");
        driver.findElement(By.cssSelector("#password")).sendKeys("123456");
        driver.findElement(By.cssSelector("#registerForm > button")).click();
        // 处理弹窗
        wait.until(ExpectedConditions.alertIsPresent());
        Alert alert = driver.switchTo().alert();
        String actualText = alert.getText();
        String expectText = "注册成功，去登录！";
        Assertions.assertEquals(expectText, actualText);
        alert.accept();
        // 跳转到登录页面
        String actUrl = driver.getCurrentUrl();
        System.out.println("用户登录页面url: " + actUrl);
        String expectUrl = "http://82.156.185.199:8080/blogin.html";
        Assertions.assertEquals(expectUrl, actUrl);
    }

    // 处理弹窗
    public void alterHandle(){
        wait.until(ExpectedConditions.alertIsPresent());
        Alert alert1 = driver.switchTo().alert();
        String actualText1 = alert1.getText();
        System.out.println("弹窗文本：" + actualText1);
        String expectText1 = "注册失败！undefined";
        Assertions.assertEquals(expectText1, actualText1);
        alert1.accept();
    }
}
